package util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender extends Thread {
	
	private String miCorreo = "alumni.itba@gmail.com";
    private String miContrasena = "itba1234";
    private final String gmailServidorSMTP = "smtp.gmail.com";
    private final String itbaServidorSMTP = "mail.alu.itba.edu.ar";
    private final String puertoEnvio = "465";
	private String from = miCorreo;
	private String[] receptors;
	private MailTitleBody mtb;
    
    public EmailSender(String from, String[] mailReceptor, MailTitleBody mtb) {
    	this(mailReceptor, mtb);
    	this.from = from;
	}
    
	public EmailSender(String mailReceptor, MailTitleBody mtb) {
		this(new String[] {mailReceptor}, mtb);
	}
	    
	public EmailSender(String[] mailReceptor, MailTitleBody mtb) {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream("src/main/resources/mail.properties"));
            this.miCorreo = prop.getProperty("mail.account");
			this.miContrasena = prop.getProperty("mail.password");
		} catch (IOException ex) {
			ex.printStackTrace();
	    }
		this.receptors = mailReceptor;
		this.mtb = mtb;
		this.from = miCorreo;
	}

	public void run() {
        Properties props = new Properties();
        props.put("mail.smtp.user", miCorreo);
        if (miCorreo.contains("gmail.com")) {
        	props.put("mail.smtp.host", gmailServidorSMTP);
	        props.put("mail.smtp.port", puertoEnvio);
	        props.put("mail.smtp.starttls.enable", "true");
	        props.put("mail.smtp.auth", "true");
	        props.put("mail.smtp.socketFactory.port", puertoEnvio);
	        props.put("mail.smtp.socketFactory.class",
	                "javax.net.ssl.SSLSocketFactory");
	        props.put("mail.smtp.socketFactory.fallback", "false");
        } else {
    		props.put("mail.smtp.host", itbaServidorSMTP);
    		props.put("mail.smtp.port", "25");
    		//TODO: not working
        }
        @SuppressWarnings("unused")
		SecurityManager security = System.getSecurityManager();

        try {
            Authenticator auth = new AutentificadorSMTP();
            Session session = Session.getInstance(props, auth);

            if (mtb.getPasses() != null && mtb.getPasses().length > 0) {
            	int i=0;
            	for (String pass: mtb.getPasses()) {
            		try {
	            		MimeMessage msg = new MimeMessage(session);
	                    msg.setSubject(mtb.getTitle());
	                    msg.setContent(mtb.getBody() + pass, "text/html; charset=ISO-8859-1");
	                    msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(receptors[i++]));
	                    Transport.send(msg);
	            	} catch (Exception mex) {
	                    mex.printStackTrace();
	                }
            	}
            } else {
            	MimeMessage msg = new MimeMessage(session);
                msg.setSubject(mtb.getTitle());
                msg.setContent(mtb.getBody(), "text/html; charset=ISO-8859-1");
                msg.setReplyTo(new Address[] {new InternetAddress(from)});
                msg.saveChanges();
                for (String mailReceptor: receptors) {
                	msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(mailReceptor));
                }
                Transport.send(msg);
            }
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    private class AutentificadorSMTP extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(miCorreo, miContrasena);
        }
    }

}
